package com.bangyo.common.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sothis.core.panelet.impl.ServletPaneletRequest;

import com.bangyo.core.manager.UserContext;
import com.bangyo.core.manager.impl.PaneletUserContext;
import com.bangyo.core.persister.EntityPersister;
import com.bangyo.user.ApplicationUser;

/**
 * 系统管理员授权过滤器。禁止未经授权的用户访问后台系统管理平台。
 * 
 * @author Topbit 2006-5-23 15:47:22
 */
public class AdministratorAuthorizationFilter extends AbstractHttpFilter
{
    private EntityPersister<ApplicationUser> applicationUserPersister = null;

    @SuppressWarnings("unchecked")
    public void doHttpFilter(HttpServletRequest request,
        HttpServletResponse response, FilterChain chain) throws IOException,
        ServletException
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("requestURI=["+request.getRequestURI()+"].");
        }

        UserContext context = PaneletUserContext
            .getUserContext(new ServletPaneletRequest(request));
        if (0L==context.getUserPoin())
        {
            if (logger.isInfoEnabled())
            {
                logger.info("Anonymous user access, remoteAddress=["
                    +context.getRemoteAddress()+"].");
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "Anonymous user is denied.");
            return;
        }

        ApplicationUser user = applicationUserPersister.getByKey(context
            .getUserPoin());
        if (null==user)
        {
            if (logger.isInfoEnabled())
            {
                logger.info("User does not exist with poin=["
                    +context.getUserPoin()+"], remoteAddress=["
                    +context.getRemoteAddress()+"].");
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "Cheat session.");
            return;
        }

        if (user.isLoginDisabled())
        {
            if (logger.isInfoEnabled())
            {
                logger.info("User can not login with poin=["
                    +context.getUserPoin()+"], loginName=["+user.getLoginName()
                    +"], remoteAddress=["+context.getRemoteAddress()+"].");
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "User is disabled to login.");
            return;
        }

        if (user.isDefunct())
        {
            if (logger.isInfoEnabled())
            {
                logger.info("User is defunct with poin=["+context.getUserPoin()
                    +"], loginName=["+user.getLoginName()+"], remoteAddress=["
                    +context.getRemoteAddress()+"].");
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "User is locked.");
            return;
        }

        if (!user.isAdminUser())
        {
            if (logger.isInfoEnabled())
            {
                logger.info("User is not an administrator with poin=["
                    +context.getUserPoin()+"], loginName=["+user.getLoginName()
                    +"], remoteAddress=["+context.getRemoteAddress()+"].");
            }
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                "User is not an administrator.");
            return;
        }

        chain.doFilter(request, response);
    }

    public void setApplicationUserPersister(
        EntityPersister<ApplicationUser> applicationUserPersister)
    {
        this.applicationUserPersister = applicationUserPersister;
    }

}